//
//  LogInViewController.m
//  Anyair
//
//  Created by karlcool on 14-5-12.
//  Copyright (c) 2014年 Karlcool. All rights reserved.
//

#import "LogInViewController.h"
#import "Regular.h"
#import "AuthViewController.h"
#import "MainViewController.h"

@interface LogInViewController ()

@end

@implementation LogInViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = AIR_GRAY;
    
    LRTopBar *topBar = [[LRTopBar alloc]initWithTitle:@"登陆" AndLeftText:@"取消" AndRightText:@"提交"];
    topBar.delegete = self;
    [self.view addSubview:topBar];
    
    UIView *phoneBackView = [[UIView alloc]initWithFrame:CGRectMake(-0.5, 100, 321, 100)];
    phoneBackView.backgroundColor = [UIColor whiteColor];
    phoneBackView.layer.borderColor = BREAKLINE_GRAY.CGColor;
    phoneBackView.layer.borderWidth = 0.5;
    [self.view addSubview:phoneBackView];
    
    phoneText = [[UITextField alloc]initWithFrame:CGRectMake(25, 10, 245, 30)];
    phoneText.backgroundColor = [UIColor clearColor];
    phoneText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    phoneText.placeholder = @"手机号";
    phoneText.keyboardType = UIKeyboardTypePhonePad;
    [phoneText addTarget:self action:@selector(didEndExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [phoneBackView addSubview:phoneText];    
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"userPhone"]!=nil) {
        phoneText.text = [[NSUserDefaults standardUserDefaults]objectForKey:@"userPhone"];
    }
    
    passwordText = [[UITextField alloc]initWithFrame:CGRectMake(25, 60, 245, 30)];
    passwordText.backgroundColor = [UIColor clearColor];
    passwordText.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    passwordText.placeholder = @"密码";
    passwordText.secureTextEntry = YES;
    [passwordText addTarget:self action:@selector(didEndExit:) forControlEvents:UIControlEventEditingDidEndOnExit];
    [phoneBackView addSubview:passwordText];
    
    breakLine = [[UIView alloc]initWithFrame:CGRectMake(25, 50, 295, 1)];
    breakLine.backgroundColor = BREAKLINE_GRAY;
    [phoneBackView addSubview:breakLine];
    
    
    findBtn = [[UIButton alloc]initWithFrame:CGRectMake(200, phoneBackView.frame.origin.y+phoneBackView.frame.size.height-5, 100, 50)];
    findBtn.backgroundColor = [UIColor clearColor];
    findBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    findBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [findBtn setTitle:@"找回密码" forState:UIControlStateNormal];
    [findBtn setTitleColor:AIR_GREEN forState:UIControlStateNormal];
    [findBtn setTitleColor:AIR_GREEN_DEEP forState:UIControlStateHighlighted];
    [findBtn addTarget:self action:@selector(clickFind:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:findBtn];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 找回密码
- (void)clickFind:(UIButton *)sender {
    AuthViewController *find = [[AuthViewController alloc]init];
    [self presentViewController:find animated:YES completion:nil];
}
#pragma mark - 释放键盘
- (void)didEndExit:(UITextField *)sender {
    [sender resignFirstResponder];
}

#pragma mark - 顶栏委托

#pragma mark - 取消登陆
- (void)clickLeft:(UIButton*)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark - 执行登陆
- (void)clickRight:(UIButton*)sender {
    if (![Regular validateMobile:phoneText.text]) {
        UIAlertView *phoneAlert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"手机号输入有误" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [phoneAlert show];
        return;
    }
    if (passwordText.text.length==0) {
        UIAlertView *passwordAlert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"密码不能为空" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [passwordAlert show];
        return;
    }
    if (![Regular validatePassword:passwordText.text]) {
        UIAlertView *passwordAlert = [[UIAlertView alloc]initWithTitle:@"错误" message:@"密码必须为6到16为数字和字母" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [passwordAlert show];
        return;
    }
    if (![LRTools checkNetWork]) {
        UIAlertView *failAlert = [[UIAlertView alloc]initWithTitle:@"网络不可用" message:@"请检查你的网络" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        [failAlert show];
        return;
    }
    //登陆操作
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_LOW ,0),^{
        
        NSDictionary *resposeDic = [[LRHttpManager createHttpManager]logInUserByPhone:phoneText.text AndPassword:passwordText.text];
        dispatch_async (dispatch_get_main_queue(),^{
            
            if ([[resposeDic objectForKey:@"code"] integerValue]==0) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                [[NSUserDefaults standardUserDefaults]setObject:phoneText.text forKey:@"userPhone"];
                [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"isLogIn"];
                [LRUserManager createManager].isLogIn = YES;
                [LRUserManager createManager].userPhone = phoneText.text;
                
                if (self.tempLogIn) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:^{
                        
                    }];
                } else {
                    [self.navigationController dismissViewControllerAnimated:NO completion:^{
                        [[NSNotificationCenter defaultCenter]postNotificationName:@"logInSuccess" object:nil];
                    }];
                }
                
                

                
            } else if ([[resposeDic objectForKey:@"code"] integerValue]==1) {
                [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
                if ([[resposeDic objectForKey:@"message"] isEqualToString:@"密码错误"]) {
                    MBProgressHUD *shortHub = [[MBProgressHUD alloc]initWithView:self.view];
                    [self.view addSubview:shortHub];
                    [shortHub setLabelText:@"密码错误"];
                    [shortHub setMode:MBProgressHUDModeText];
                    [shortHub hide:YES afterDelay:1];
                    [shortHub show:YES];
                } else {
                    MBProgressHUD *shortHub = [[MBProgressHUD alloc]initWithView:self.view];
                    [self.view addSubview:shortHub];
                    [shortHub setLabelText:@"登陆失败,请稍后再试"];
                    [shortHub setMode:MBProgressHUDModeText];
                    [shortHub hide:YES afterDelay:1];
                    [shortHub show:YES];
                }
                
                
            }
            
        });
    });
    
    
}

@end
